package org.minbox.framework.microservice.order.api;

import org.minbox.framework.microservice.common.entity.GoodInfo;
import org.minbox.framework.microservice.common.entity.OrderInfo;
import org.minbox.framework.microservice.common.entity.UserInfo;
import org.minbox.framework.microservice.openfeign.GoodServiceClient;
import org.minbox.framework.microservice.openfeign.UserServiceClient;
import org.minbox.framework.microservice.order.OrderDetail;
import org.minbox.framework.microservice.order.service.OrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 订单控制器
 *
 * @author 恒宇少年
 */
@RestController
@RequestMapping(value = "/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    /**
     * 用户服务客户端
     */
    @Autowired
    private UserServiceClient userServiceClient;
    /**
     * 商品服务客户端
     */
    @Autowired
    private GoodServiceClient goodServiceClient;

    @GetMapping(value = "/{orderNumber}")
    public OrderDetail selectBasicInfo(@PathVariable String orderNumber) {
        // 本服务内OrderService，查询出订单的基本信息
        OrderInfo orderInfo = orderService.findByNumber(orderNumber);
        OrderDetail orderDetail = new OrderDetail();
        BeanUtils.copyProperties(orderInfo, orderDetail);
        // 用户服务查询用户基本信息
        UserInfo userInfo = userServiceClient.findById(orderDetail.getUserId());
        orderDetail.setUser(userInfo);
        // 商品服务查询商品基本信息
        GoodInfo goodInfo = goodServiceClient.findById(orderDetail.getGoodId());
        orderDetail.setGood(goodInfo);
        return orderDetail;
    }
}
